package com.hqyj.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 注解@RequestBody：主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }

    /**
     * 注解@ModelAttribute
     */
    @PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }

    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId) {
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     * localhost/api/city/1890
     * 注解@PathVariable：接受@GetMapping("/city/{cityId}")的占位符时使用 (@PathVariable int cityId)
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }

    /**
     * localhost/api/cities/522
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId) {
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * localhost/api/cities
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }

    /**
     * localhost/batch/cities
     * {"cityName":"dream1", "localCityName":"梦幻之城1", "countryId":"522"},{"cityName":"dream2", "localCityName":"梦幻之城2", "countryId":"522"}
     */
    @PostMapping(value = "/batch/cities", consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<City> cities) {
        return cityService.insertCities(cities);
    }
}
